package com.aaa.controller.tables;

import com.aaa.dao.ActivityDao;
import com.aaa.dao.ActivityMapper;
import com.aaa.dao.ApplyDateDao;
import com.aaa.dao.InventoryMapper;
import com.aaa.mq.RabbitmqListener;
import com.aaa.service.ActivityService;
import com.aaa.service.impl.ActivityServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wsc.entity.Activity;
import com.wsc.entity.FOrderp;
import com.wsc.entity.Inventory;
import com.wsc.entity.ResultMsg;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("activity")
@RestController
public class ActivityController {
    @Resource
    ActivityServiceImpl activityService;
    @Resource
    ActivityDao activityDao;
    @Resource
    ActivityMapper activityMapper;
    @Resource
    ApplyDateDao applyDateDao;
    @RequestMapping("addActivity")
    public int addActivity(Activity activity){
        return activityService.addActivity(activity);
    }

    @RequestMapping("select")
    @Cacheable(value = "activity",key = "'find'")
    //@CachePut
    //@CacheEvict
    public List<Activity> select(){
        List<Activity> list=activityDao.select1();
        return list;
    }

    @RequestMapping("select1")
    public Object select1(){
        return activityMapper.selectList(null);
    }

    @RequestMapping("list")
    public ResultMsg list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Activity> list = activityService.list();
        PageInfo<Activity> pageInfo = PageInfo.of(list);
        return ResultMsg.success(pageInfo);
    }
    @RequestMapping("insert")
    public int insert(Activity activity){
        System.out.println(activity);
        activity.setStatus(0);
        activity.setVersion(1);
        Integer num= activity.getNums();
        Inventory inventory=new Inventory();//减库存
        inventory.setSurplus(activity.getNums());
        inventory.setBook(activity.getBid());
        applyDateDao.update2(inventory);
        return activityService.addActivity(activity);
    }

    @RequestMapping("update")
    public ResultMsg update(Activity activity) {
        Integer oldNum= activityDao.selected(activity.getAid());//原有抢购数量
        if(oldNum<activity.getNums()) {//旧的抢购数量少于新的抢购数量，新的多，要额外减库存
            Inventory inventory = new Inventory();//减库存
            inventory.setSurplus(activity.getNums()-oldNum);
            inventory.setBook(activity.getBid());
            applyDateDao.update2(inventory);
            int i = activityService.update(activity);
            return ResultMsg.success(i);
        }else {//当新修改的图书抢购数量少于旧的抢购数量，旧的多，要将之前多删掉的库存增加回来
            Inventory inventory = new Inventory();//加库存
            inventory.setSurplus(oldNum-activity.getNums());//activity.getNums()代表新的库存
            inventory.setBook(activity.getBid());
            applyDateDao.update3(inventory);
            int i = activityService.update(activity);
            return ResultMsg.success(i);
        }
    }

//    @RequestMapping("update")
//    public Integer update(Activity activity) {
//        System.out.println("这是"+activity);
//        return activityMapper.updateById(activity);
//    }

    @RequestMapping("delete")
    public Integer delete(Integer aid){
        return activityMapper.deleteById(aid);
    }

    @RequestMapping("removeById")
    public ResultMsg removeById(Integer aid) {
        return ResultMsg.success(activityService.removeById(aid));
    }

    @RequestMapping("selects")
    public Integer selects(Integer bid){
        return activityDao.selects(bid);
    }

    @RequestMapping("update2")//减库存
    public Integer update2(Activity activity){
        Inventory inventory=new Inventory();
        inventory.setSurplus(activity.getNums());
        inventory.setBook(activity.getBid());
        return applyDateDao.update2(inventory);
    }

}
